//
//  DDNewJobsViewController.m
//  DingDongZP
//
//  Created by Ray on 16/6/7.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDNewJobsViewController.h"
#import "DDDiscoverJobCell.h"
#import "DDWorkDetailViewController.h"
#import "DDJobsService.h"
#import "DDJobsBaseModel.h"
#import "DDJobfinderDetailInfo.h"
#import "DDCommonDataModel.h"
#import "DDUserInfoService.h"

@interface DDNewJobsViewController ()
<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UILabel *lbNull;
@property (nonatomic, strong) UITableView *tbHot;

@property (nonatomic, strong) DDJobsService *jobsService;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation DDNewJobsViewController

- (void)dealloc
{
    [self hideLoading];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新职位";
    
    self.lbNull = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    self.lbNull.font = [UIFont systemFontOfSize:17.0];
    self.lbNull.textColor = COLOR_FONT_LIGHT_GRAY;
    self.lbNull.text = @"暂无数据";
    self.lbNull.textAlignment = NSTextAlignmentCenter;
    self.lbNull.center = CGPointMake(self.view.width/2, self.view.height/2);
//    self.lbNull.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleBottomMargin;
    self.lbNull.hidden = YES;
    [self.view addSubview:_lbNull];
    
    _tbHot = [[UITableView alloc] init];
    _tbHot.tag = 2;
    _tbHot.delegate = self;
    _tbHot.dataSource = self;
    _tbHot.backgroundColor = COLOR_BACKGROUND_GRAY;
    _tbHot.rowHeight = 182;
    _tbHot.tableFooterView = [[UIView alloc] init];
    
    UINib *cellNib = [UINib nibWithNibName:@"DDDiscoverJobCell" bundle:nil];
    [_tbHot registerNib:cellNib forCellReuseIdentifier:DDDiscoverJobCellIdentifier];
    
    _tbHot.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tbHot.frame = self.view.bounds;
    _tbHot.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tbHot];
    
    [self.view bringSubviewToFront:_lbNull];
    
    self.jobsService = [[DDJobsService alloc] init];
    self.dataSource = [NSMutableArray new];
    [self loadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - data loading
- (void)loadData {
    
    [self showLoading];
    
    WEAKSELF
    
    [self.jobsService queryHotestOrLatestJobsWithPage:@"0" order:@"latest" successBlock:^(id responseObject) {
        
        [weakSelf hideLoading];
        if ([weakSelf isValidateResponseData:responseObject]) {
            // 请求成功
            DDJobsBaseModel *model = [DDJobsBaseModel yy_modelWithJSON:responseObject[@"data"]];
            
            if ([model.jobs count] == 0) {
                weakSelf.lbNull.hidden = NO;
            } else {
                weakSelf.lbNull.hidden = YES;
                [weakSelf hideLoading];
            }
            
            [weakSelf.dataSource addObjectsFromArray:model.jobs];
            [weakSelf.tbHot reloadData];
            
        } else {
            [weakSelf responseUnValidate:responseObject];
        }
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}

#pragma mark -
#pragma mark  UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDDiscoverJobCell *cell;
    cell = (DDDiscoverJobCell *)[tableView dequeueReusableCellWithIdentifier:DDDiscoverJobCellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DDDiscoverJobCell" owner:self options:nil] lastObject];
    }
    
    if ([self.dataSource count] > indexPath.row) {
        [cell updateViewWithData:(DDJobsDetail *)self.dataSource[indexPath.row]];
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row < self.dataSource.count) {
        DDJobsDetail *detail = self.dataSource[indexPath.row];
        DDWorkDetailViewController *workVC = [[DDWorkDetailViewController alloc] init];
        workVC.title = detail.jobtitle;
        workVC.jobDetail = detail;
        workVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:workVC animated:YES];
        
        //  主动聊天增加活跃度
        [[DDUserInfoService sharedService] addActivityWithType:kActivityCommunicate callback:^(BOOL succ, id data) {
            
        }];
    }
}

@end
